$(function() {
  var $reporter = $('#reporter');
  var $uploadPhotos = $('.upload-photos');
  var claimImgs = [];
  var sending = false;

  $('.radio-group').on('click', '.rt-checkbox', function() {
    var $this = $(this)
    if ($this.hasClass('checked')) return
    $this.siblings().removeClass('checked')
    $this.addClass('checked')
  })

  $('.report-type').on('click', '.rt-checkbox', function() {
    var val = $(this).data('value');
    if (val == 1) {
      $reporter.show();
    } else {
      $reporter.hide();
    }
  });

  $('.xieyi .rt-checkbox').on('click', function() {
    $(this).toggleClass('checked')
  });

  $('#upload').on('change', function(e) {
    var file = e.target.files[0];
    uploadImage(file);
  })

  $('.submit-button').on('click', submit)

  function getPostData() {
    var travelMode = $('select[name="travelMode"]').val();
    if (travelMode === '0') {
      alert('请选择旅游方式');
      return;
    }

    var travelType = $('select[name="travelType"]').val();
    if (travelType === '0') {
      alert('请选择旅游类别');
      return;
    }

    var claimTarget = $('select[name="claimTarget"]').val();
    if (claimTarget === '0') {
      alert('请选择投诉对象');
      return;
    }

    var claimCnt = $('textarea[name="claimCnt"]').val();
    console.log(claimCnt)
    if (claimCnt.trim() === '') {
      alert('请填写投诉事由');
      return;
    }

    return {
      travelMode: travelMode,
      travelType: travelType,
      claimTarget: claimTarget,
      claimCnt: claimCnt,
      realAnony: $('.report-type').find('.checked').data('value'),
      claimName: $('input[name="claimName"]').val(),
      gender: $('.sexy-radio-group').find('.checked').data('value'),
      contactPhone: $('input[name="contactPhone"]').val(),
      idType: $('select[name="idType"]').val(),
      idNo:$('input[name="idNo"]').val(),
    }
  }

  function submit() {
    if (sending) return;
    if (!$('#isAgree').hasClass('checked')) {
      alert('请选择同意协议内容');
      return;
    }
    var apiUrl = 'http://218.205.115.243:18080/afot-web/submitTourComplaint'
    var data = getPostData()
    console.log(data)
    sending = true;
    $.ajax({
      type: 'POST',
      url: apiUrl,
      data: data,
      contentType: 'application/json; charset=utf-8',
      dataType: 'json',
      success: function(ret) {
        sending = false
        if (ret.operStatus == 1) {
          alert('提交成功');
          setTimeout(function() {
            window.close();
          }, 3000);
        } else {
          alert(ret.operMsg)
        }
      },
      error: function(err) {
        sending = false
        alert('提交失败，请重试')
      }
    })
  }

  function uploadImage(file) {
    var url = 'http://218.205.115.243:18080/afot-web/uploadImg';
    var formData = new FormData();
    formData.append('files', file)
    formData.append('userId', 1)
    $.ajax({
      type: 'post',
      url: url,
      data: formData,
      contentType: false,
      processData: false,
      dataType: 'json',
      success: function(ret) {
        claimImgs.push(ret.data[0]);
        previewImg(file);
      },
      error: function(err) {
        alert('上传失败')
      }
    })
  }

  function previewImg(file) {
    var reader = new FileReader();
    reader.onload = function(result) {
      var img = document.createElement('img');
      img.setAttribute('src', result.target.result);
      $uploadPhotos.append(img)
      if ($uploadPhotos.children().length >= 3) {
        $('#upload').hide();
      }
    }
    reader.readAsDataURL(file);
  }
})
